
<?php

@session_start();

require(dirname(__FILE__) . "/../lib/idverify.inc");
require(dirname(__FILE__) . "/../lib/common.inc");

define("ls_nolog", 0,true);        // 登录成功
define("ls_logged_noact", 1,true); // 无提交，已登录
define("ls_nolog_noact", 2,true);  // 无提交, 未登录
define("ls_logged_1", 3,true);     // 重复登录，登录同名
define("ls_logged_2", 4,true);     // 重复登录,登录不同名
define("ls_failedcheck_nolog", 5,true); // 验证失败，之前无登录
define("ls_failedcheck_logged", 6,true);// 验证失败，之前有登陆

$login_status = ls_nolog_noact;
$pre_username = "";
$req_username = "";
$rtn_username = "";
$username = "";

if (isset($_SESSION['username'])) { // 已经登陆
	if ( trim($_SESSION['username']) == "" ) {
		session_unset();
		unset($_SESSION['username']);
	}
}

if( !isset($_POST['username']) ) { // 未输入登录信息
	if (isset($_SESSION['username'])) { // 已经登陆
		$login_status = ls_logged_noact;
		$pre_username = $_SESSION['username'];
		$req_username = "";
		$rtn_username = $_SESSION['username'];
	}else {                             // 尚未登录
		$login_status = ls_nolog_noact;
		$pre_username = "";
		$req_username = "";
		$rtn_username = "";
	}
} else {                                                        // 已输入登录信息
	
	$username = htmlspecialchars($_POST['username']);
	$password = htmlspecialchars($_POST['password']);
	
	if (idverify($username, $password)){// 登录成功
		if (isset($_SESSION['username'])) { // 已经登陆
			if ($_SESSION['username'] == $username){
				$login_status = ls_logged_1;
				$pre_username = $_SESSION['username'];
				$req_username = $username;
				$rtn_username = $pre_username;
			} else {
				$login_status = ls_logged_2;
				$pre_username = $_SESSION['username'];
				$req_username = $username;
				$rtn_username = $pre_username;;
			}
		} else {                            // 尚未登录
			$_SESSION['username'] = $username;
			$login_status = ls_nolog;
			$pre_username = "";
			$req_username = $username;
			$rtn_username = $username;
		}
	}else{                            // 登录失败
		if (isset($_SESSION['username'])) { // 登录失败，之前已经登录
			$login_status = ls_failedcheck_logged;
			$pre_username = $_SESSION['username'];
			$req_username = $username;
			$rtn_username = $pre_username;
		} else {                            // 验证失败，之前无登录
			$login_status = ls_failedcheck_nolog;
			$pre_username = "";
			$req_username = $username;
			$rtn_username = "";
		}
	}
}

$rtn_user = false;
if ($rtn_username != '') {
	$rtn_user = get_user_info($rtn_username);
}

$s_json_response = "";
$s_json_response .= '{ ';

$s_json_response .= '"' . 'pre_username' . '": ';
$s_json_response .= '"' . $pre_username  . '", ';

$s_json_response .= '"' . 'req_username' . '": ';
$s_json_response .= '"' . $req_username  . '", ';

$s_json_response .= '"' . 'rtn_username' . '": ';
$s_json_response .= '"' . $rtn_username  . '", ';

if ($rtn_user) {
	$s_json_response .= '"' . 'rtn_avatar' . '": ';
	$s_json_response .= '"' . $rtn_user['avatar']  . '", ';

	$s_json_response .= '"' . 'rtn_intro' . '": ';
	$s_json_response .= '"' . $rtn_user['introduction']  . '", ';
}

$s_json_response .= '"' . 'status' . '": ';
$s_json_response .= '"' . strval($login_status)  . '"';

$s_json_response .= ' }';

print($s_json_response);

?>
